package com.senseuni.da.ui

import android.content.Context
import android.media.AudioManager
import android.os.Bundle
import android.provider.Settings
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.SeekBar
import androidx.fragment.app.Fragment
import com.senseuni.da.R
import com.senseuni.da.databinding.FragmentNotificationBinding
import com.senseuni.da.ui.dialog.SleepSettingDialog
import com.senseuni.da.utils.SpUtil


class NotificationFragment : Fragment() {

    companion object {
        fun newInstance() = NotificationFragment()
    }

    private lateinit var binding: FragmentNotificationBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentNotificationBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        binding.snivNotification.setOnClickListener {
            var enable = binding.snivNotification.isChecked()
            binding.snivNotification.setChecked(!enable)
            enable = binding.snivNotification.isChecked()
            SpUtil.saveShowNotification(enable)
            binding.snivNotificationNewImage.isEnabled = enable
            binding.snivNotificationNewFriend.isEnabled = enable
            binding.snivNotificationUpgrade.isEnabled = enable
        }

        binding.snivNotificationNewImage.setOnClickListener {
            var enable = binding.snivNotificationNewImage.isChecked()
            binding.snivNotificationNewImage.setChecked(!enable)
            enable = binding.snivNotificationNewImage.isChecked()
            SpUtil.saveShowNotificationNewImage(enable)
        }

        binding.snivNotificationNewFriend.setOnClickListener {
            var enable = binding.snivNotificationNewFriend.isChecked()
            binding.snivNotificationNewFriend.setChecked(!enable)
            enable = binding.snivNotificationNewFriend.isChecked()
            SpUtil.saveShowNotificationNewFriend(enable)
        }

        binding.snivNotificationUpgrade.setOnClickListener {
            var enable = binding.snivNotificationUpgrade.isChecked()
            binding.snivNotificationUpgrade.setChecked(!enable)
            enable = binding.snivNotificationUpgrade.isChecked()
            SpUtil.saveShowNotificationUpgrade(enable)
        }
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        val enableNotify = SpUtil.isShowNotification()
        binding.snivNotification.setChecked(enableNotify)
        binding.snivNotificationNewImage.isEnabled = enableNotify
        binding.snivNotificationNewFriend.isEnabled = enableNotify
        binding.snivNotificationUpgrade.isEnabled = enableNotify

        binding.snivNotificationNewFriend.setChecked(SpUtil.isShowNotificationNewFriend())
        binding.snivNotificationNewImage.setChecked(SpUtil.isShowNotificationNewImage())
        binding.snivNotificationUpgrade.setChecked(SpUtil.isShowNotificationUpgrade())

        val audioManager = context?.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        binding.seekbarVolume.max =
            audioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION)

        val volume = audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION)
        binding.seekbarVolume.progress = volume
        binding.seekbarVolume.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, progress, 0)
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
            }
        })

    }


}